package his.controller;

import com.alibaba.fastjson.JSONObject;
import his.entity.Register;
import his.entity.User;
import his.entity.Voucher;
import his.service.RegisterService;
import his.vo.DepartmentVO;
import his.vo.MedicalRecordVO;
import his.vo.PayVO;
import his.vo.RegisterVO;
import org.example.his.common.api.CommonResponse;
import org.example.his.common.utils.RequestToJsonObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.List;

@Controller
@RequestMapping("/regis/")
public class RegisterController {

    @Autowired
    RegisterService registerService;

    @GetMapping("check_token")
    @ResponseBody
    public CommonResponse check_token(@RequestParam String token){
        return registerService.check_token(token);
    }

    @GetMapping("get_casenumber")
    @ResponseBody
    public CommonResponse<String> get_casenumber(@RequestParam String token){
        return registerService.get_casenumber(token);
    }

    @GetMapping("search/{CaseNumber}")
    @ResponseBody
    public CommonResponse<List<Register>> get_search_register(@PathVariable  String CaseNumber){
        return registerService.get_searchregister(CaseNumber);
    }
     //删除
    @PostMapping("delRegister")
    @ResponseBody
    public CommonResponse<Register> del_regist(HttpServletRequest httpServletRequest){
        JSONObject jsonObject= RequestToJsonObject.RequestToJsonObject(httpServletRequest);
        String CaseNumber=(String) jsonObject.get("CaseNumber");
        int VisitState = (int)jsonObject.get("VisitState");
        return registerService.dele_regist(CaseNumber , VisitState);
    }

//    @PostMapping("patientCosts")
//    @ResponseBody
//    public CommonResponse<List<MedicalRecordVO>> search_VO(@RequestParam String CaseNumber){
//        return registerService.search_patient_cost(CaseNumber);
//    }

    @GetMapping("patientCosts/{CaseNumber}")
    @ResponseBody
    public CommonResponse<List<MedicalRecordVO>> search_VO(@PathVariable String CaseNumber){
//        JSONObject jsonObject= RequestToJsonObject.RequestToJsonObject(httpServletRequest);
//        String CaseNumber=(String) jsonObject.get("CaseNumber");
        System.out.println(CaseNumber);
        return registerService.search_patient_cost(CaseNumber);
    }

    @PostMapping("getvoucher")
    @ResponseBody
    public CommonResponse<List<Voucher>> getvoucher(HttpServletRequest httpServletRequest){
        JSONObject jsonObject= RequestToJsonObject.RequestToJsonObject(httpServletRequest);
        String CaseNumber=(String) jsonObject.get("CaseNumber");
        return registerService.getvoucher(CaseNumber);
    }

    @GetMapping("payfunc")
    @ResponseBody
    public CommonResponse pay_func( @RequestParam String casenumber){

        return registerService.pay_func(casenumber);
    }

//    @PostMapping("pay")
//    @ResponseBody
//    public CommonResponse<List<MedicalRecordVO>> pay( HttpServletRequest httpServletRequest){
//        JSONObject jsonObject= RequestToJsonObject.RequestToJsonObject(httpServletRequest);
//        String CaseNumber=(String) jsonObject.get("CaseNumber");
//        return registerService.pay(CaseNumber);
//    }


    @PostMapping("regist")
    @ResponseBody
    public CommonResponse<RegisterVO> new_regist(HttpServletRequest httpServletRequest){
        JSONObject jsonObject= RequestToJsonObject.RequestToJsonObject(httpServletRequest);
        String RealName=(String) jsonObject.get("RealName");
        String IDnumber=(String) jsonObject.get("IDnumber");
        String Gender=(String) jsonObject.get("Gender");
        int Age =  Integer.parseInt((String) jsonObject.get("Age"));
        String BirthDate = (String)jsonObject.get("BirthDate");
        String HomeAddress = (String)jsonObject.get("HomeAddress");
        int RegistLeID = Integer.parseInt((String) jsonObject.get("RegisLeID"));
        int DeptID = Integer.parseInt((String) jsonObject.get("DeptId"));
        int UserID = Integer.parseInt((String) jsonObject.get("UserId"));
        int RegisterID = Integer.parseInt((String) jsonObject.get("RegisterID"));
        String isBook = (String) jsonObject.get("isBook");
        char isBook1 = isBook.charAt(0);
        String Noon = (String)jsonObject.get("Noon");
        return registerService.add_new_register(RealName , Gender , Age , BirthDate , IDnumber , HomeAddress, RegistLeID , DeptID , UserID , isBook1 ,RegisterID ,Noon);
    }

    @PostMapping("pay")
    @ResponseBody
    public CommonResponse<PayVO> pay(HttpServletRequest httpServletRequest){
        JSONObject jsonObject= RequestToJsonObject.RequestToJsonObject(httpServletRequest);
        String CaseNumber=(String) jsonObject.get("CaseNumber");
        int ItemID = (int)jsonObject.get("ItemID");
        int RegistID = (int)jsonObject.get("RegistID");
        BigDecimal totalPrice = (BigDecimal) jsonObject.get("totalPrice");
        int ItemType = (int)jsonObject.get("ItemType");
        return registerService.pay(CaseNumber , ItemID , RegistID , totalPrice ,ItemType);
    }

    @GetMapping("get_depar")
    @ResponseBody
    public CommonResponse<List<DepartmentVO>> get_depar_lists(){
        return registerService.get_depar_lists();
    }

    @GetMapping("get_doc")
    @ResponseBody
    public CommonResponse<List<User>> get_doctors(@RequestParam int depar_id){
        return registerService.get_doctors(depar_id);
    }


    @PostMapping("regist_xcx")
    @ResponseBody
    public CommonResponse<String> regist_xcx(HttpServletRequest httpServletRequest){
        JSONObject jsonObject= RequestToJsonObject.RequestToJsonObject(httpServletRequest);
        int doc_id = (int) jsonObject.get("doc_id");
        String token=(String) jsonObject.get("token");
        return registerService.regist_xcx(doc_id , token);
    }
    @PostMapping("refund")
    @ResponseBody
    public CommonResponse<String> refunds(HttpServletRequest httpServletRequest){
        JSONObject jsonObject= RequestToJsonObject.RequestToJsonObject(httpServletRequest);
        int RegistID =(int)jsonObject.get("RegistID");
        int ItemID = (int) jsonObject.get("ItemID");
        int ItemType = (int)  jsonObject.get("ItemType");
        String CaseNumber = (String) jsonObject.get("CaseNumber");
        BigDecimal price = (BigDecimal)jsonObject.get("price");
        String Status = (String) jsonObject.get("Status");
        BigDecimal totalPrice =(BigDecimal) jsonObject.get("totalPrice");
//        int RegistID , String CaseNumber,int ItemID, BigDecimal price, int ItemType, String Status ,BigDecimal totalPrice
        return registerService.refunds(RegistID , CaseNumber , ItemID ,price ,ItemType ,Status ,totalPrice);
    }

}
